package sfeir.authform.client;

import sfeir.authform.client.dao.AuthUser;
import sfeir.authform.client.forms.CreationForm;
import sfeir.authform.client.forms.LoginForm;
import sfeir.authform.client.service.AuthGWTService;
import sfeir.authform.client.service.AuthGWTServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class Authform implements EntryPoint {

    public void onModuleLoad() {

        // CREATION FORM
        CreationForm create = new CreationForm() {

            @Override
            public void sendLogin(String login, String passwd) {
                if (checkEmailFormat()){

                    AuthGWTServiceAsync service = GWT.create(AuthGWTService.class);
                    AuthUser user = new AuthUser();
                    user.setEmail(login);
                    user.setPassword(passwd);
                    service.signup(user, new AsyncCallback<Boolean>(){

                        @Override
                        public void onFailure(Throwable caught) {
                            
                        }

                        @Override
                        public void onSuccess(Boolean result) {
                            if(result){
                                Window.alert("Profile created.");
                            }
                        }
                        
                    });
                }
                else
                    Window.alert("Enter a valid email");
            }

        };
        RootPanel.get("content").add(new HTML("<hr/>REGISTER FORM<hr/>"));
        RootPanel.get("content").add(create);

        // LOGIN
        LoginForm f2 = new LoginForm("login", "pass", "enter") {
            @Override
            public void sendLogin(String login, String passwd) {

                AuthGWTServiceAsync service = GWT.create(AuthGWTService.class);
                if (checkEmailFormat()) {
                    service.login(login, passwd, new AsyncCallback<AuthUser>() {

                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert("ERROR : " + caught.getMessage());
                        }

                        @Override
                        public void onSuccess(AuthUser result) {
                            if(result!=null)
                                Window.alert(result.getEmail()+" you're logged in with "+result.getPassword());
                            else{
                                Window.alert("unkown user");
                            }
                        }

                    });
                } else
                    Window.alert("Enter a valid email");
            }
        };
        f2.setLoginOpenidEnabled(true);
        f2.setLoginGoogleEnabled(true);
        f2.setLoginYahooEnabled(true);
        f2.setForgottenPasswordEnabled(true);
        RootPanel.get("content").add(new HTML("<hr/>LOGIN : Forgotten password + other ways to log in."));
        RootPanel.get("content").add(new HTML("<span class='small'>This form uses GWT services, see sendLogin() method implementation for details</span><hr/>"));
        RootPanel.get("content").add(f2);

    }
}
